package com.xiaoshuidi.cloud.module.member.mq.listerner;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventData;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventListener;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventTypeEnum;
import com.xiaoshuidi.cloud.framework.systemevent.core.data.LeaseSystemEventMobileModify;
import com.xiaoshuidi.cloud.module.member.service.activities.MemberActivitiesEnrollService;
import com.xiaoshuidi.cloud.module.member.service.card.MemberUserBankCardService;
import com.xiaoshuidi.cloud.module.member.service.comment.MemberCommentService;
import com.xiaoshuidi.cloud.module.member.service.coupon.CouponCustomerService;
import com.xiaoshuidi.cloud.module.member.service.invite.InviteActivityLogService;
import com.xiaoshuidi.cloud.module.member.service.message.MemberMessageService;
import com.xiaoshuidi.cloud.module.member.service.point.MemberPointsService;
import com.xiaoshuidi.cloud.module.member.service.questionnaireinfo.MemberAnswerService;
import com.xiaoshuidi.cloud.module.member.service.topics.MemberTopicsPostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;

@Component
@Slf4j
public class LeaseSystemEventMobileModifyListener implements LeaseSystemEventListener {
    @Resource
    private MemberActivitiesEnrollService memberActivitiesEnrollService;
    @Resource
    private MemberAnswerService memberAnswerService;

    @Resource
    private MemberCommentService memberCommentService;

    @Resource
    private CouponCustomerService couponCustomerService;

    @Resource
    private InviteActivityLogService inviteActivityLogService;

    @Resource
    private MemberMessageService memberMessageService;

    @Resource
    private MemberPointsService memberPointsService;

    @Resource
    private MemberTopicsPostService memberTopicsPostService;

    @Resource
    private MemberUserBankCardService memberUserBankCardService;

    @Resource
    private TransactionDefinition transactionDefinition;

    @Resource
    private PlatformTransactionManager transactionManager;


    @Override
    public LeaseSystemEventTypeEnum getEventType() {
        return LeaseSystemEventTypeEnum.MOBILE_MODIFY;
    }

    @Override
    public void onEvent(LeaseSystemEventData event) {
        LeaseSystemEventMobileModify data = (LeaseSystemEventMobileModify) event;
        log.info("收到修改手机消息 {}#######################", JsonUtils.toJsonString(data));
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
        try {
            if (ObjectUtils.isEmpty(data.getUserId())) {
                log.info("新用户id为空，ignore");
            } else {

                //1.活动报名表修改
               // memberActivitiesEnrollService.updateUserId(data.getOldUserId(), data.getUserId());
                //2.问卷调查
               // memberAnswerService.updateUserId(data.getOldUserId(), data.getUserId());
                //3.评价表修改
               // memberCommentService.updateUserId(data.getOldUserId(), data.getUserId());
                //4.优惠券
                couponCustomerService.updateUserId(data.getOldUserId(), data.getUserId(), data.getUserName(), data.getMobile(), data.getOldMobile());
                //5.邀请
                inviteActivityLogService.updateUserId(data.getOldUserId(), data.getUserId(), data.getMobile());
                //6.消息
               // memberMessageService.updateUserId(data.getOldUserId(), data.getUserId());
                //7.积分信息
               // memberPointsService.updateUserId(data.getOldUserId(), data.getUserId());
                //8.社区发帖点赞
              //  memberTopicsPostService.updateUserId(data.getOldUserId(), data.getUserId());
                //9.开户行修改
              //  memberUserBankCardService.updateUserId(data.getOldUserId(), data.getUserId());
                transactionManager.commit(transactionStatus);

            }
        } catch (Exception e) {
            log.error("修改手机消息处理异常", e.getMessage());
            transactionManager.rollback(transactionStatus);
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "修改手机消息处理异常!");
        }
    }
}
